home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 4.iso
/
public
/
bit
/
src
/
holo.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-08-01
|
10KB
|
437 lines
/*
* $Id: holo.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $
*
*. This file is part of BIT shareware package. After the two weeks of
* free evaluation period, you are encouraged (required) to register
* your copy for a small registration fee, which is $35 for personal use
* and $50 for commercial, government and institutional use.
*
* Copyright(c) 1993, 1994 by T.C. Zhao.
* All rights reserved.
*
* Permission to use, copy, and distribute this software in its entirety
* for non-commercial purposes is hereby granted, provided that the
* above shareware and copyright notices and this permission notice
* appear in all copies and their documentation.
*
* This software may be modified for your own use, but modified versions
* may not be distributed without prior consent of the author.
*
* This software is provided "as is" without expressed or implied
* warranty of any kind.
*
*.
*
* Read in X-ray Photo-emission data file and generate intensity maps
* (Hologram)
* default: Raster is assumed to be from LEFT to RIGHT, TOP to
* BOTTOM. Aspect ratio will be kept as the input.
*
* To force Xsize==Ysize, use @ASP. To force top to bottom and LEFT to
* right(rot90), use @YX To force Bottom to TOP , use @B2T (rot180)
*
* Note:
*
* All matrix are arranged as Matrix[Y][X], i.e.; X runs first
*/
#if !defined(lint) && defined(F_ID)
char *id_holo = "$Id: holo.c,v 0.91 1994/02/20 00:52:53 zhao Pre-Release $";
#endif
#include "bit.h"
#ifdef ADD_HOLO
#include "extern.h"
#include "dmalloc.h"
static void holo_getmap(IPTR, FILE *, int);
extern void *interpol2(ci_t **, int, int, int, int, int);
static int raw, cmin, cmax;
static char tmpstr[120];
static xyequal = 0, yx = 0, t2b = 1, mag = 0;
static int lindex = 1;
int
HOLO_desc(IPTR im)
{
int i, j, err = 0;
int xs, ys;
FILE *fp = im->fp;
if (Badfgets(tmpstr, sizeof(tmpstr) - 1, fp))
return -1;
raw = (strncmp(tmpstr, "DSBB", 4) == 0);
/* dimension must follow */
fgets(tmpstr, sizeof(tmpstr), fp);
if (sscanf(tmpstr, " %d %d ", &xs, &ys) != 2)
{
Bark("HOLO_desc", "Error getting desc");
return -1;
}
while (fgets(tmpstr, sizeof(tmpstr), fp) && tmpstr[0] == '#')
;
err += (sscanf(tmpstr, "%d %d", &i, &j) != 2);
while (fgets(tmpstr, sizeof(tmpstr), fp) && tmpstr[0] == '#')
;
err += (sscanf(tmpstr, "%d %d", &i, &j) != 2);
while (fgets(tmpstr, sizeof(tmpstr), fp) && tmpstr[0] == '#')
;
err += (sscanf(tmpstr, "%d %d", &cmin, &cmax) != 2);
while ((i = getc(fp)) != EOF && i == '#')
{
while (getc(fp) != '\n')
;
}
ungetc(i, fp);
im->w = xs;
im->h = ys;
im->colors = im->cmap->colors = (cmax - cmin + 1);
return err ? -1 : 0;
}
/* for input size smaller than {hw}_min, interpolate to def_{hw} */
static int w_min = 201, h_min = 201;
static int def_w = 221, def_h = 221;
#include <stdlib.h>
int
HOLO_load(IPTR im)
{
int i, j, err = 0;
float fcc;
int nh, nw;
int xfac, yfac, factor;
ci_t **pp, **pp1;
FILE *fp = im->fp;
float xunit = 1.0, yunit = 1.0;
int xori = 0, yori = 0;
long rlines;
rlines = progress_report("Loading Hologram ...", im->h);
pp1 = im->mraster;
if (raw)
{
for (j = 0; j < im->h; j++)
{
REPORT(j, rlines);
for (i = 0; i < im->w; i++)
pp1[j][i] = getc(fp);
}
}
else
{
for (j = 0; j < im->h; j++)
{
REPORT(j, rlines);
for (i = 0; i < im->w; i++)
{
err += ((fcc = readpint(fp)) < 0);
if (fcc > cmax)
fcc = cmax;
if (fcc < cmin)
fcc = cmin;
if (cmax - PCMAXV)
fcc *= ((float) PCMAXV / cmax);
pp1[j][i] = fcc;
}
if (err)
{
fprintf(stderr, "total %d errors encountered\n", err);
remove_progress_report();
return -1;
}
}
}
im->mraster = pp1;
get_bit_defmap(lindex, im->cmap->ct[0],
im->cmap->ct[1],
im->cmap->ct[2],
im->cmap->colors);
im->type = lindex ? T_CMAP : T_GMAP;
(void) fill_image_struct(im, pp1, im->h, im->w, im->type);
/* misc. info. All starts with @ */
xyequal = yx;
mag = 0;
t2b = 1;
while (fgets(tmpstr, sizeof(tmpstr), fp))
{
if (strncmp(tmpstr, "@MAP", 4) == 0)
{
int nentries;
err = 0;
sscanf(tmpstr + 5, "%d", &nentries);
holo_getmap(im, fp, nentries);
}
else if (strncmp(tmpstr, "@ASP", 4) == 0)
{
xyequal = 1;
}
else if (strncmp(tmpstr, "@YX", 3) == 0)
{
yx = 1;
}
else if (strncmp(tmpstr, "@B2T", 4) == 0)
{
t2b = 0;
}
else if (strncmp(tmpstr, "@MAG", 4) == 0)
{
mag = 1;
}
else if (strncmp(tmpstr, "@XUNIT", 6) == 0)
{
xunit = atof(tmpstr + 7);
}
else if (strncmp(tmpstr, "@YUNIT", 6) == 0)
{
yunit = atof(tmpstr + 7);
}
else if (strncmp(tmpstr, "@X0", 3) == 0)
{
xori = atoi(tmpstr + 4);
}
else if (strncmp(tmpstr, "@Y0", 3) == 0)
{
yori = atoi(tmpstr + 4);
}
else if (strncmp(tmpstr, "#T", 2) == 0)
{ /* text */
fseek(fp, -(long) strlen(tmpstr), SEEK_CUR);
load_text(im);
}
else if (strncmp(tmpstr, "#S", 2) == 0)
{ /* text */
fseek(fp, -(long) strlen(tmpstr), SEEK_CUR);
load_sgf(im);
}
}
set_marking_units(1.0 / xunit, 1.0 / yunit);
set_marking_origin(xori, yori);
/* we are officially done. Now process options */
pp = 0;
if (mag)
{ /* always keep aspect ratio */
xfac = def_w / im->w;
yfac = def_h / im->h;
factor = (xfac > yfac) ? yfac : xfac;
nw = im->w * (factor ? factor : 1);
nh = im->h * (factor ? factor : 1);
if (nw - im->w || nh - im->h)
img_scale(im, nh, nw, 0, 0, 0);
}
else
{
if (xyequal || im->w == im->h)
{
nw = (im->w < w_min) ? def_w : im->w;
nh = (im->h < h_min) ? def_h : im->h;
}
else
{
xfac = def_w / im->w;
yfac = def_h / im->h;
factor = (xfac > yfac) ? yfac : xfac;
nw = im->w * (factor ? factor : 1);
nh = im->h * (factor ? factor : 1);
}
if (nw - im->w || nh - im->h)
{
/*
* could've been using scale_hifi, but we want the
* interpolation to happen for the index ....
*/
pp = interpol2(pp1, im->h, im->w, nh, nw, 1);
}
}
remove_progress_report();
if (pp || pp1 != im->mraster)
{
sprintf(tmpstr, "(was %-dX%-d)", im->w, im->h);
strcpy(im->misc, tmpstr);
}
return pp ? fill_image_struct(im, pp, nh, nw, im->type) : 0;
}
/* ARGSUSED */
const char *
HOLO_wdefault(const IPTR im)
{
return raw ? "Raw" : "Ascii";
}
/* ARGSUSED */
int
HOLOdump_init(IPTR im)
{
raw = !raw;
return 0;
}
/* write to disk */
int
HOLO_dump(IPTR im)
{
register ci_t *p = im->raster, *q;
register int i;
register pc_t *gr = im->cmap->ct[3];
int gmap = 0; /* always zero for the moment BUG */
FILE *fp = im->fp;
int ckk = (PCMAX < 1000);
int per_line = ckk ? 16 : 14;
const char *fmt = ckk ? "%4d" : "%5d";
fprintf(fp, "%s\n%d %d\n 1 %d\n 1 %d\n 0 %d\n",
raw ? "DSBB" : "DSAA", im->w, im->h, im->w, im->h, (PCMAX - 1));
show_busy("Writing ...");
pack_cmap_g(im->cmap);
if (!raw)
{
for (i = 0, q = p + im->w * im->h; p < q; p++, i++)
{
fprintf(fp, fmt, gmap ? (int) gr[*p] : (int) *p);
if ((i + 1) % per_line == 0)
putc('\n', fp);
}
if (i % per_line != 0)
putc('\n', fp);
}
else
{
for (q = p + im->w * im->h; p < q; p++)
{
putc(gmap ? gr[*p] : *p, fp);
}
}
end_busy();
return fflush(fp);
}
static void
holo_getmap(IPTR im, FILE * fp, int n)
{
int i, j, c, err = 0;
unsigned char cb[PCMAX];
if (n > PCMAXV || n <= 0)
{
M_err("HoloMap", "Bad number of colors");
return;
}
err = 0;
if (!raw)
{
for (j = 0; j < 3; j++)
{
for (i = 0; i < n; i++)
{
err += (fscanf(fp, "%d", &c) != 1);
im->cmap->ct[j][i] = c;
}
}
}
else
{
for (j = 0; j < 3; j++)
{
err += (fread(cb, 1, n, fp) != n);
for (i = 0; i < n; i++)
im->cmap->ct[j][i] = cb[i];
}
}
if (err)
{
M_err("HoloMap", "Error readin colormaps\n", stderr);
return;
}
return;
}
/* cycle thru local maps */
void
HOLO_special(IPTR im)
{
if (IS_CI(im))
{
++lindex;
lindex %= get_number_of_defmaps();
get_bit_defmap(lindex, im->cmap->ct[0],
im->cmap->ct[1], im->cmap->ct[2], im->cmap->colors);
im->type = lindex ? T_CMAP : T_GMAP;
set_cmap(im->cmap);
}
}
/*
* general 2-D linear interpolation where old and new grids are mismatched,
* i.e., (or+1)/in != integer
*/
void *
interpol2(ci_t **in, int ir, int ic, int or, int oc, int rep)
{
register int i, j, kx, ky, kx1, ky1;
register float dx, dy, f1, f2, f, sx, sy;
register float *flookup;
register float tmp;
register ci_t **m;
int rlines;
if (ir <= 1 || ic <= 1 || or <= 1 || oc <= 1 || !in)
{
fputs("Bad parameter in interpol\n", stderr);
return 0;
}
if ((m = get_mat(or, oc, sizeof(ci_t))) == 0)
return 0;
if ((flookup = malloc(sizeof(float) * (oc + 1))) == 0)
return 0;
sx = (float) (ir - 1) / (or - 1);
sy = (float) (ic - 1) / (oc - 1);
for (i = 0; i < oc; i++)
flookup[i] = sy * i;
rlines = rep ? progress_report("Interpolating ...", or) : 10000;
/* f(x+dx,y+dy)=f(x,y)+f'|y dx + f'|x dy + f'' dxdy */
for (i = 0; i < or; i++)
{
if (rep)
{
REPORT(i, rlines);
}
dx = sx * i;
kx = dx;
kx1 = (kx == ir - 1) ? kx : kx + 1;
dx = dx - kx;
for (j = 0; j < oc; j++)
{
ky = flookup[j];
ky1 = ky + (ky < ic - 1);
dy = flookup[j] - ky;
tmp = (float) in[kx][ky];
f1 = (float) in[kx1][ky] - tmp;
f2 = (float) in[kx][ky1] - tmp;
f = tmp + dx * f1 + dy *
(f2 + dx * (in[kx1][ky1] - tmp - f1 - f2)) + 0.6;
m[i][j] = (ci_t) f;
}
}
free(flookup);
remove_progress_report();
return m;
}
#endif